<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CommHub extends CI_Controller {

	public function CommHub()
	{
		parent::__construct();
		$this->load->model('file_model');
		mb_internal_encoding("UTF-8");
		$this->config->load('awb');
	}
	public function get_group()
	{
		$user = $this->ion_auth->user()->row();
		if($user){
			$this->load->model('group_model');
			$group = $this->group_model->get_group_by_user_id($user->pk_id);
			echo json_encode($group);
		}
		else
			echo false;
	}
	public function open_workspace(){
		$this->load->library('ion_auth');
		$this->load->model('group_model');
		$user = $this->ion_auth->user()->row();
		$group = $this->group_model->get_group_by_user_id($user->pk_id);
		$files = new stdClass();
		// if(!empty($group))
		// {
			// $group = $group[0];
			// $files->file = $this->group_model->get_file_by_workspace_id($group->workspace_id);
			// $workspace = $this->group_model->get_workspace_by_id($group->workspace_id);
		// }
		$temp_files = array();
		foreach($group as $single_group)
		{
			$group_file = $this->group_model->get_file_by_group_id($single_group['AWBGROUP_ID'],true);
			foreach($group_file as $single_group_file)
			{
				array_push($temp_files,$single_group_file);
			}
		}
		$files->file = $temp_files;
		$record = null;
		$file_array = array();
		// print_r($files->file);
		foreach($files->file as  $value){ 
			if(!$value->is_template)
				$file_array[$value->file_id] = $value->file_name;
		}
		natcasesort($file_array);		
		foreach($file_array as $key=> $value){ 
		 $record .= "<option value=".$key.">".$value."</option>";
		}
		echo json_encode($record); 
	}
	public function open_workspace_template(){
		$this->load->library('ion_auth');
		$this->load->model('group_model');
		$user = $this->ion_auth->user()->row();
		$group = $this->group_model->get_group_by_user_id($user->pk_id);
		$files = new stdClass();
		// if(!empty($group))
		// {
			// $group = $group[0];
			// $files->file = $this->group_model->get_file_by_workspace_id($group->workspace_id);
			// $workspace = $this->group_model->get_workspace_by_id($group->workspace_id);
		// }
		$temp_files = array();
		foreach($group as $single_group)
		{
			$group_file = $this->group_model->get_file_by_group_id($single_group['AWBGROUP_ID']);
			foreach($group_file as $single_group_file)
			{
				array_push($temp_files,$single_group_file);
			}
		}
		$files->file = $temp_files;
		$record = null;
		$file_array = array();
		foreach($files->file as $value){ 
				if($value->is_template)
				$file_array[$value->file_id] = $value->file_name;
		} 
		natcasesort($file_array);		
		foreach($file_array as $key=> $value){ 
		 $record .= "<option value=".$key.">".$value."</option>";
		}
		echo json_encode($record); 
	}
	public function file_uploader(){
		$this->load->library('Jquery_upload_wrapper');
	}
	public function recover_object()
	{
		if(!$this->ion_auth->logged_in())
		{
			echo false;
			return;
		}
		$history_id = $this->input->get("history_id");
		$file_id = $this->input->get("file_id");
		$page_id = $this->input->get("page_id");
		$history_data = $this->file_model->get_history_by_id($history_id);
		if(!empty($history_data))
		{
			if($history_data->operation == "DEL")
			{
				$recovered = $this->file_model->is_already_recovered($history_id);
				if(!$recovered)
				{
					$user = $this->ion_auth->user()->row();
					$data = array();
					$data["history_id"] = $history_id;
					$data["user_id"] = $user->id;
					$this->file_model->add_recover_history($data);
					$recover_obj = $history_data->previous_content;
					$file = $this->file_model->get_file_by_file_id($file_id);
					$file_content = $file->content;
					
				
					$json_decoded = json_decode($file_content);
					
					
					foreach($json_decoded->pages as $page)
					{
						if($page->id == $page_id)
						{
							array_push($page->drawing_objects,json_decode($recover_obj));
						}
					}
					
					
					$json_encoded = json_encode($json_decoded);
					
					$saved_file["content"] = $json_encoded;
					$this->file_model->save($saved_file, $file_id);
					echo $recover_obj;
					return;
				}
			}
		}
		echo false;
		
	}
	
	public function recover_object_undo()
	{
		if(!$this->ion_auth->logged_in())
		{
			echo false;
			return;
		}
		$history_id = $this->input->get("history_id");
		$file_id = $this->input->get("file_id");
		$page_id = $this->input->get("page_id");
		$history_data = $this->file_model->get_history_by_id($history_id);
		if(!empty($history_data))
		{
			if($history_data->operation == "DEL")
			{
				
				$user = $this->ion_auth->user()->row();
				$recover_obj = $history_data->previous_content;
				$file = $this->file_model->get_file_by_file_id($file_id);
				$file_content = $file->content;
				
			
				$saved_file_h["file_id"] 			= $file_id;
				$saved_file_h["operation"] 			= "ADD";
				$saved_file_h["updated_time"] 		= time();
				$saved_file_h["changed_content"] 	= $recover_obj;
				$saved_file_h["previous_content"] 	= "";
				$saved_file_h["changed_object_id"] 	= $history_data->changed_object_id;
				$saved_file_h["user_id"] 	= $user->id;
				$saved_file_h["page_id"] 	= $page_id;
				$this->file_model->save_file_history($saved_file_h);
				
				$json_decoded = json_decode($file_content);
				
				
				foreach($json_decoded->pages as $page)
				{
					if($page->id == $page_id)
					{
						array_push($page->drawing_objects,json_decode($recover_obj));
					}
				}
				
				
				$json_encoded = json_encode($json_decoded);
				
				$saved_file["content"] = $json_encoded;
				$this->file_model->save($saved_file, $file_id);
				echo $recover_obj;
				return;
				
			}
		}
		echo false;
		
	}
	public function recover_object_redo()
	{
		if(!$this->ion_auth->logged_in())
		{
			echo false;
			return;
		}
		$history_id = $this->input->get("history_id");
		$file_id = $this->input->get("file_id");
		$page_id = $this->input->get("page_id");
		$history_data = $this->file_model->get_history_by_id($history_id);
		if(!empty($history_data))
		{
			if($history_data->operation == "ADD")
			{
				
				$user = $this->ion_auth->user()->row();
				$recover_obj = $history_data->changed_content;
				$file = $this->file_model->get_file_by_file_id($file_id);
				$file_content = $file->content;
				
				$saved_file_h["file_id"] 			= $file_id;
				$saved_file_h["operation"] 			= "ADD";
				$saved_file_h["updated_time"] 		= time();
				$saved_file_h["changed_content"] 	= $recover_obj;
				$saved_file_h["previous_content"] 	= "";
				$saved_file_h["changed_object_id"] 	= $history_data->changed_object_id;
				$saved_file_h["user_id"] 	= $user->id;
				$saved_file_h["page_id"] 	= $page_id;
				$this->file_model->save_file_history($saved_file_h);
				
				$json_decoded = json_decode($file_content);
				
				
				foreach($json_decoded->pages as $page)
				{
					if($page->id == $page_id)
					{
						array_push($page->drawing_objects,json_decode($recover_obj));
					}
				}
				
				
				$json_encoded = json_encode($json_decoded);
				
				$saved_file["content"] = $json_encoded;
				$this->file_model->save($saved_file, $file_id);
				echo $recover_obj;
				return;
				
			}
		}
		echo false;
		
	}
	public function delete_file_workspace(){
		$file_id = $this->input->post("file_id");
		if (!is_dir('documents/'.$file_id)) {
			mkdir('documents/'.$file_id);
		}
		$dirPath='documents/'.$file_id;
		 $objects = scandir($dirPath); 
        foreach ($objects as $object) { 
        if ($object != "." && $object != "..") { 
            if (filetype($dirPath."/".$object) == "dir"){
                 deleteDirectory($dirPath."/".$object);
            }else{
                 unlink($dirPath."/".$object);
            } 
        } 
        reset($objects); 
        rmdir($dirPath);
		}		
		$this->file_model->delete($file_id);
	}
	
	public function chart_uploader(){
		$this->load->library('jpgraph_pi');
	}
	public function delete_page(){
		$page_id = $this->input->post('page_id');
		$file_id = $this->input->post("file_id");
		$file = $this->file_model->get_file_by_file_id($file_id);
		if(!empty($page_id))
		{		
			$json_object = json_decode($file->content); 
			$deleted_page_id=-1;
			$update_time = time();
			//print_r($json_object);
			foreach($json_object->pages as $page_key => $single_page)
			{
				if($single_page->id == $page_id)
				{	
					$deleted_page_id = $page_id;
					break;
				}
			}
			// echo $deleted_page_id;
			
			// print_r($json_object);
			if($deleted_page_id != -1)
			{
				//update history
				$this->file_model->delete_page_history($file_id,$page_id);
				$hist = $this->file_model->get_file_history_since(0,$file_id);
				foreach($hist as $single_hist)
				{
					if($single_hist->page_id > $deleted_page_id)
					{
						$single_hist->page_id--;
						$this->file_model->update_history((array)$single_hist);
					}
				}
				$deleted_page_id--;
				// print_r($json_object->pages[$deleted_page_id]);
				if(sizeof($json_object->pages) <= 1)
				{
					echo false;
					return;
				}
				//delete();
				unlink("documents/".$file_id."/thumb-".$page_id.".png");
				unlink("documents/".$file_id."/background-".$page_id.".png");
				unset($json_object->pages[$deleted_page_id]);
				for($i=$deleted_page_id+1;$i<=sizeof($json_object->pages);$i++)
				{
					$json_object->pages[$i]->id--;
					rename("documents/".$file_id."/thumb-".($json_object->pages[$i]->id+1).".png","documents/".$file_id."/thumb-".($json_object->pages[$i]->id).".png");
					rename("documents/".$file_id."/background-".($json_object->pages[$i]->id+1).".png","documents/".$file_id."/background-".($json_object->pages[$i]->id).".png");
				}
				$json_object->pages=array_values($json_object->pages);
			
				$new_content = json_encode($json_object);
				$file->content = $new_content;
				$this->file_model->save($file,$file_id);
				
				
				echo true;
				
			}
			else
			{
				echo false;
			}
			
		}
	}
		
	public function update_object_property(){
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$object_json = $this->input->post("object_json");
		$object_json = json_decode($object_json);
	
		
		$file = $this->file_model->get_file_by_file_id($file_id);
		$json_object = json_decode($file->content); 
		$original_drawing_object=null;
		$changed_drawing_object=null;
		$update_time = time(); 
		foreach($json_object->pages as $page_key => $single_page)
		{ 
			if($single_page->id == $page_id)
			{	
				foreach($single_page->drawing_objects as $drawing_key => $single_drawing_object)
				{
					if($single_drawing_object->id == $object_json->id)
					{ 
						$original_drawing_object = clone $single_drawing_object;
					
						$single_drawing_object->posy = $object_json->posy;
						$single_drawing_object->posx = $object_json->posx;
						
						if($single_drawing_object->type == "text" || $single_drawing_object->type == "buble" || $single_drawing_object->type == "sticky-text" ){
							
							if(property_exists($object_json, 'content'))
								$single_drawing_object->content = $object_json->content;
							if($single_drawing_object->type == "sticky-text")
							{	
								if(property_exists($object_json, 'background'))
									$single_drawing_object->background = $object_json->background;
							}
						}
						$single_drawing_object->width = $object_json->width;
						$single_drawing_object->height = $object_json->height;
						$single_drawing_object->last_update = $update_time;
						
						$changed_drawing_object= $single_drawing_object;
						break;
					}
				}
			}
		}
		$new_content = json_encode($json_object);
		$file->content = $new_content;
		$this->file_model->save($file,$file_id);
		$user = $this->ion_auth->user()->row();
		$changed_drawing_object = json_encode($changed_drawing_object);
			
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "EDIT";
		$saved_file_h["updated_time"] 		= $update_time;
		$saved_file_h["changed_content"] 	= $changed_drawing_object;
		$saved_file_h["previous_content"] 	= json_encode($original_drawing_object);
		$saved_file_h["changed_object_id"] 	= $object_json->id;
		$saved_file_h["user_id"] 	= $user->id;
		$saved_file_h["page_id"] 	= $page_id;
		$this->file_model->save_file_history($saved_file_h);
		
		echo json_encode(array('status'=>true,'message'=>'update success','history'=>$saved_file_h));
	}
	
	public function update_chart_property()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id"); 
		$object_id =$this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy"); 
		$data_chart =$this->input->post("data_chart");
		$type_chart =$this->input->post("type_chart");
		$width_chart =$this->input->post("width_chart");
		$height_chart =$this->input->post("height_chart");
		$gradient =$this->input->post("gradient");
		$axisCrd =$this->input->post("axisCrd");
		$start_color =$this->input->post("start_color");
		$end_color =$this->input->post("end_color");
		$fill_color =$this->input->post("fill_color");
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
	
		
		$file = $this->file_model->get_file_by_file_id($file_id);
		$json_object = json_decode($file->content); 
		$original_drawing_object=null;
		$changed_drawing_object=null;
		$update_time = time(); 
		
		foreach($json_object->pages as $page_key => $single_page)
		{ 
			if($single_page->id == $page_id)
			{	
				foreach($single_page->drawing_objects as $drawing_key => $single_drawing_object)
				{
					if($single_drawing_object->id == $object_id)
					{ 
						// $original_drawing_object = $single_drawing_object; 
						$original_drawing_object = clone $single_drawing_object;
						
						$single_drawing_object->posy = $posx;
						$single_drawing_object->posx = $posy; 
						
						if($single_drawing_object->type == "chart"){ 
							$this->chart_uploader(); 
							$ydata = $data_chart;   
							// echo $ydata;
							$graph = $this->jpgraph_pi->chartgraph($ydata, 'DigitalBoard', $type_chart, $width_chart, $height_chart, $axisCrd, $fill_color,$gradient, $start_color, $end_color); 
							 
							$graph_temp_directory = 'temp_upload';  
							$graph_file_name = $object_id.'_'.time().'.png';  
							$graph_file_location = $graph_temp_directory . '/' . $graph_file_name; 
							
							$documents_chart = 'documents/'.$file_id.'/'.$graph_file_name;
		
							$graph->Stroke('./'.$documents_chart);  // create the graph and write to file
							
							$data['graph'] = $documents_chart; 
							// $graph->Stroke('./'.$graph_file_location);  // create the graph and write to file
							
							// $data['graph'] = $graph_file_location; 
							// print_r($data['graph']);
							$single_drawing_object->image_url = $documents_chart; 
							$single_drawing_object->data_chart = implode(",", $data_chart); 
							$single_drawing_object->type_chart = $type_chart; 
							$single_drawing_object->color1 = $fill_color; 
							$single_drawing_object->color2 = $start_color; 
							$single_drawing_object->color3 = $end_color; 
							// print_r($graph_file_location);
						}
						$single_drawing_object->width = $width_chart;
						$single_drawing_object->height = $height_chart;
						$single_drawing_object->last_update = $update_time;
						$single_drawing_object->object_type = "chart";
						$single_drawing_object->created_by = $username;
						$single_drawing_object->created_date = $curr_date;
						$single_drawing_object->gradient = $gradient;
						
						
						$changed_drawing_object= $single_drawing_object;
						break;
					}
				}
			}
		} 
		
		$new_content = json_encode($json_object);
		$file->content = $new_content;
		$this->file_model->save($file,$file_id);
			
		$changed_drawing_object = json_encode($changed_drawing_object);
			
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "EDIT";
		$saved_file_h["updated_time"] 		= $update_time;
		$saved_file_h["changed_content"] 	= $changed_drawing_object;
		$saved_file_h["previous_content"] 	= json_encode($original_drawing_object);
		$saved_file_h["changed_object_id"] 	= $object_id;
		$saved_file_h["page_id"] 	= $page_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		$this->file_model->save_file_history($saved_file_h);
		 
		echo json_encode(array('status'=>true,'message'=>$changed_drawing_object,'history'=>$saved_file_h));
	
	}
	public function update_image()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$image_data = $this->input->post('image_data');
		$path = $this->input->post('filepath');
		$object_id = $this->input->post("id");
		$old_id = $this->input->post("old_id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");
		$obj_type =$this->input->post("obj_type");
		$image_data = explode('base64,',$image_data); 
		$size = file_put_contents($path, base64_decode($image_data[1]));
	
		$sizes = getimagesize($path);
	
		$return_data['width'] = $sizes[0];
		$return_data['height'] = $sizes[1];
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $old_id;
		$return_data['type'] = "image";
		$return_data['image_url'] = $path;
		
		$file = $this->file_model->get_file_by_file_id($file_id);
		$json_object = json_decode($file->content); 
		$original_drawing_object=null;
		$changed_drawing_object=null;
		$update_time = time(); 
		
		foreach($json_object->pages as $page_key => $single_page)
		{ 
			if($single_page->id == $page_id)
			{	
				foreach($single_page->drawing_objects as $drawing_key => $single_drawing_object)
				{
					if($single_drawing_object->id == $old_id)
					{ 
						// $original_drawing_object = $single_drawing_object; 
						$original_drawing_object = clone $single_drawing_object;
						
						
						$single_drawing_object->posy = $posx;
						$single_drawing_object->posx = $posy; 
						
						$single_drawing_object->width =  $sizes[0];
						$single_drawing_object->height =  $sizes[1];
						$single_drawing_object->last_update = time();
						$single_drawing_object->image_url = $path;
						
						
						$changed_drawing_object= $single_drawing_object;
						break;
					}
				}
			}
		} 
		
		$new_content = json_encode($json_object);
		$file->content = $new_content;
		$this->file_model->save($file,$file_id);
			
		$changed_drawing_object = json_encode($changed_drawing_object);
			
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "EDIT";
		$saved_file_h["updated_time"] 		= $update_time;
		$saved_file_h["changed_content"] 	= $changed_drawing_object;
		$saved_file_h["previous_content"] 	= json_encode($original_drawing_object);
		$saved_file_h["changed_object_id"] 	= $old_id;
		$saved_file_h["page_id"] 	= $page_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		$this->file_model->save_file_history($saved_file_h);
		 
		echo json_encode(array('status'=>true,'message'=>$changed_drawing_object,'history'=>$saved_file_h));
	}
	
	public function update_table()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id"); 
		$object_id =$this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");	
		$width =$this->input->post("width");
		$height =$this->input->post("height");
		$content_table =$this->input->post("content_table");
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
		$border_style =$this->input->post("border_style");
		$style =$this->input->post("style");
		$row_table =$this->input->post("row_table");
		$col_table =$this->input->post("col_table");
		$thickness =$this->input->post("thickness");
		
		$return_data['width'] = $width;
		$return_data['height'] = $height;
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $object_id;
		$return_data['type'] = "table";
		$return_data['content_table'] = $content_table;
		$return_data['object_type'] = "table";
		$return_data['created_by'] = $username;
		$return_data['created_date'] = $curr_date;
		$return_data['border_style'] = $border_style;
		$return_data['row_table'] = $row_table;
		$return_data['col_table'] = $col_table;
		$return_data['thickness'] = $thickness;
		$return_data['style'] = $style;
		
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
		$json_decoded = json_decode($file->content);
		
		$original_drawing_object=null;
		$changed_drawing_object=null;
		$update_time = time(); 
		
		foreach($json_decoded->pages as $page_key => $single_page)
		{ 
			if($single_page->id == $page_id)
			{	
				foreach($single_page->drawing_objects as $drawing_key => $single_drawing_object)
				{
					if($single_drawing_object->id == $object_id)
					{ 
						$original_drawing_object = clone $single_drawing_object; 
						
						
						$single_drawing_object->posx = $posx;
						$single_drawing_object->posy = $posy; 
						
						
						$single_drawing_object->width = $width;
						$single_drawing_object->height = $height;
						$single_drawing_object->content_table = $content_table;
						$single_drawing_object->last_update = $update_time;
						$single_drawing_object->object_type = "table";
						$single_drawing_object->created_by = $username;
						$single_drawing_object->created_date = $curr_date;
						$single_drawing_object->border_style = $border_style;
						$single_drawing_object->row_table = $row_table;
						$single_drawing_object->col_table = $col_table; 
						$single_drawing_object->thickness = $thickness; 
						$single_drawing_object->style = $style; 
						
						$changed_drawing_object= $single_drawing_object;
						break;
					}
				}
			}
		} 
		 
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$changed_drawing_object = json_encode($changed_drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "EDIT";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $changed_drawing_object;
		$saved_file_h["previous_content"] 	= json_encode($original_drawing_object);
		$saved_file_h["changed_object_id"] 	= $object_id;
		$saved_file_h["page_id"] 	= $page_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		
		//print_r($saved_file_h);
		$this->file_model->save_file_history($saved_file_h);
		
		 // echo json_encode($return_data);
		echo json_encode(array('status'=>true,'message'=>json_encode($return_data),'history'=>$saved_file_h));
	}
	
	public function copy_object()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$path = $this->input->post('filepath');
		$copypath = $this->input->post('copypath');
		$object_id =$this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");
		$width =$this->input->post("width");
		$height =$this->input->post("height");
		$type =$this->input->post("type");
		$object_type =$this->input->post("object_type");
		
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
		copy( $path , $copypath );
		
		$return_data['width'] = $width;
		$return_data['height'] = $height;
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $object_id;
		$return_data['type'] = $type;
		$return_data['image_url'] = $copypath;
	
		$return_data['created_by'] = $username;
		$return_data['created_date'] = $curr_date;
		
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy;
		$drawing_object->width =  $width;
		$drawing_object->height =  $height;
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id;
		$drawing_object->image_url = $copypath;
		$drawing_object->type = $type;
		$drawing_object->object_type = $object_type;
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
		
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		$saved_file_h["page_id"] 	= $page_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		echo json_encode(array('status'=>true,'message'=>json_encode($return_data),'history'=>$saved_file_h));
		
	}
	
	public function copy_text()
	{
		$return_data=array();
		if(!$this->ion_auth->logged_in())
		{
			$return_data['success']=false;
			$return_data['error']="";
		}
		else
		{
			$user = $this->ion_auth->user()->row();
			$file_id = $this->input->post("file_id");
			$page_id = $this->input->post("page_id");
			$object_id = $this->input->post("object_id");
			$text_type = $this->input->post("text_type");
			$content = $this->input->post('content');
			$posx =$this->input->post("posx");
			$posy =$this->input->post("posy");
			$w =$this->input->post("width");
			$h =$this->input->post("height");
			$username =$user->username;
			$curr_date =date("j/n/Y");
			
		
			$file = $this->file_model->get_file_by_file_id($file_id);
			$file_content = $file->content;
			
		
			$json_decoded_file_content = json_decode($file->content);
			
			$drawing_object = new stdClass();
			$drawing_object->posx = $posx;
			$drawing_object->posy = $posy;
			$drawing_object->width =  $w;
			$drawing_object->height =  $h;
			$drawing_object->last_update = time();
			$drawing_object->id = $object_id;
			$drawing_object->content = $content;
			$drawing_object->type = $text_type;
			$drawing_object->object_type = $text_type;
			$drawing_object->created_by = $username;
			$drawing_object->created_date = $curr_date;
			if($text_type == "buble" || $text_type == "sticky-text")
			{
				$drawing_object->kind = $this->input->post('kind');
			}
			foreach($json_decoded_file_content->pages as $page)
			{
				if($page->id == $page_id)
				{
					array_push($page->drawing_objects,$drawing_object);
				}
			}
			
			
			$json_encoded_file_content = json_encode($json_decoded_file_content);
			
			$saved_file["content"] = $json_encoded_file_content;
			$this->file_model->save($saved_file, $file_id);
			
			$drawing_object = json_encode($drawing_object);
			$saved_file_h["file_id"] 			= $file_id;
			$saved_file_h["operation"] 			= "ADD";
			$saved_file_h["updated_time"] 		= time();
			$saved_file_h["changed_content"] 	= $drawing_object;
			$saved_file_h["previous_content"] 	= "";
			$saved_file_h["changed_object_id"] 	= $object_id;	
			$saved_file_h["page_id"] 	= $page_id;
			$saved_file_h["user_id"] 	= $user->id;
			
			$return_data['success']=true;
			$return_data['id']=$object_id;
			$return_data['width']=$w;
			$return_data['height']=$h;
			$return_data['posx']=$posx;
			$return_data['posy']=$posy;
			$return_data['content']=$content;			
			$return_data['object_type']=$text_type;
			$return_data['created_by']=$username;
			$return_data['created_date']=$curr_date;
			if($text_type == "buble" || $text_type == "sticky-text")
			{
				$return_data['kind']=$this->input->post('kind');
			}
		
			// $this->file_model->save_file_history($saved_file_h);
			$h_id = $this->file_model->save_file_history($saved_file_h);
			$saved_file_h["history_id"] = $h_id;
		}
		// echo json_encode($return_data);
		
		echo json_encode(array('status'=>true,'message'=>json_encode($return_data),'history'=>$saved_file_h));
	}
	public function online_check()
	{
		if($this->ion_auth->logged_in())
		{
			echo true;
		}
		echo false;
	}
	public function copy_chart()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$path = $this->input->post('filepath');
		$copypath = $this->input->post('copypath');
		$object_id =$this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");
		$width =$this->input->post("width");
		$height =$this->input->post("height");
		$type =$this->input->post("type");
		$data_chart =$this->input->post("data_chart");
		
		$user = $this->ion_auth->user()->row();
		$username =$user->username;
		$curr_date =date("j/n/Y");
		
		copy( $path , $copypath );
		
		$return_data['width'] = $width;
		$return_data['height'] = $height;
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $object_id;
		$return_data['type'] = $type;
		$return_data['image_url'] = $copypath;
		$return_data['data_chart'] = $data_chart;
		$return_data['object_type'] = "chart";
		$return_data['data_chart'] = $data_chart;
		$return_data['created_date'] = $curr_date;
		$return_data['created_by'] = $username;
		$return_data['object_type'] = "chart";
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy;
		$drawing_object->width =  $width;
		$drawing_object->height =  $height;
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id;
		$drawing_object->image_url = $copypath;
		$drawing_object->type = $type;
		$drawing_object->data_chart = $data_chart;
		$drawing_object->object_type = "chart";
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
				
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		
		//print_r($saved_file_h);
		// $this->file_model->save_file_history($saved_file_h);
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		// echo json_encode($return_data);
		
		echo json_encode(array('status'=>true,'message'=>json_encode($return_data),'history'=>$saved_file_h));
	}
	
	public function copy_table()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id"); 
		$object_id =$this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");	
		$object_type =$this->input->post("object_type");
		$width =$this->input->post("width");
		$height =$this->input->post("height");
		$content_table =$this->input->post("content_table");
		//Add by Nanda DR 25072013 - Notes: id copy object
		$copied_id =$this->input->post("copied_id");		
				
		$user = $this->ion_auth->user()->row();
		$username =$user->username;
		$curr_date =date("j/n/Y");
		
		$return_data['width'] = $width;
		$return_data['height'] = $height;
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $object_id;
		$return_data['type'] = "table";
		// $return_data['content_table'] = $content_table; //Remark by Nanda DR 25072013
		$return_data['object_type'] = "table";
		$return_data['created_date'] = $curr_date;
		$return_data['created_by'] = $username;
	 
		
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy; 
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id; 
		$drawing_object->type = "table";
		$drawing_object->object_type = "table";
		// $drawing_object->content_table = $content_table;				//Remark by Nanda DR 25072013
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				//Edit Nanda - Notes: change value content_object. get content_object from database by id object. (Copy object)
				foreach($page->drawing_objects as $drawing_key => $single_drawing_object)
				{
					if($single_drawing_object->id == $copied_id)
					{ 
						
						$return_data['style'] = $single_drawing_object->style;
						$return_data['thickness'] = $single_drawing_object->thickness;
						$return_data['content_table'] = $single_drawing_object->content_table; 
						$return_data['border_style'] = $single_drawing_object->border_style;
						$return_data['row_table'] = $single_drawing_object->row_table;
						$return_data['col_table'] = $single_drawing_object->col_table;
						
						$drawing_object->style = $single_drawing_object->style;
						$drawing_object->thickness = $single_drawing_object->thickness;
						$drawing_object->content_table = $single_drawing_object->content_table; 
						$drawing_object->border_style = $single_drawing_object->border_style;
						$drawing_object->row_table = $single_drawing_object->row_table;
						$drawing_object->col_table = $single_drawing_object->col_table;
						
						array_push($page->drawing_objects,$drawing_object);
						break;
					}
				}
				
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		$saved_file_h["page_id"] 	= $page_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		
		//print_r($saved_file_h);
		// $this->file_model->save_file_history($saved_file_h);
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		echo json_encode(array('status'=>true,'message'=>json_encode($return_data),'history'=>$saved_file_h));
		 // echo json_encode($return_data);
	}
	
	public function add_stamp()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$stamppath = $this->input->post('stamppath');
		$path = $this->input->post('filepath');
		$object_id =$this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
		
		$sizes = getimagesize($path);

		$width = $sizes[0];
		$height = $sizes[1];
		copy( $path , $stamppath );
		
		$posx -= floor($width/2);
		$posy -= floor($height/2);
		$return_data['width'] = $width;
		$return_data['height'] = $height;
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $object_id;
		$return_data['type'] = "image";
		$return_data['object_type'] = "stamp";
		$return_data['image_url'] = $stamppath;
		$return_data['created_by'] = $username;
		$return_data['created_date'] = $curr_date;
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy;
		$drawing_object->width =  $width;
		$drawing_object->height =  $height;
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id;
		$drawing_object->type = "image";
		$drawing_object->object_type = "stamp";
		$drawing_object->image_url = $stamppath;
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		$saved_file_h["page_id"] 	= $page_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		
		//print_r($saved_file_h);
		// $this->file_model->save_file_history($saved_file_h);
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		// echo json_encode($return_data);
		echo json_encode(array('status'=>true,'message'=>json_encode($return_data),'history'=>$saved_file_h));
		
	}
	
	public function add_uploaded_image()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$imagepath = $this->input->post('imagepath');
		$path = $this->input->post('filepath');
		$object_id =$this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
		//prep for tiff file
		$ext_image = end(explode('.',$path));
		if(strtolower($ext_image) == "tiff" || strtolower($ext_image) == "tif")
		{
			$imagepath.=".tiff";
		}
		else
		{
			$imagepath.=".png";
		}
		$sizes = getimagesize($path);
		
		$width = $sizes[0];
		$height = $sizes[1];
		copy( $path , $imagepath );
		
		// print_r($posx);
		$newValWidth = 0;
		$newValHeight = 0;
		$totalSizeX = $posx + $width;
		$totalSizeY = $posy + $height; 
		// print_r($width);	
		// print_r($height);	
		if($totalSizeX > 970 || $totalSizeY > 600){ 
			if($totalSizeX > 970){  
				$newValWidth = 970 - $posx; 
			}else{ 
				$newValWidth = $width;
			}
			
			if($totalSizeY > 600){ 
				$newValHeight = 600 - $posy;
			}else{
				$newValHeight = $height;
			}
		}else{
			$newValWidth = $width;
			$newValHeight = $height;
		}
		// $return_data['width'] = $width;
		// $return_data['height'] = $height;
		// print_r($newValWidth);	
		// print_r($newValHeight);
		$return_data['width'] = $newValWidth;
		$return_data['height'] = $newValHeight;
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $object_id;
		$return_data['type'] = "image";
		$return_data['image_url'] = $imagepath;
		$return_data['created_by'] = $username;
		$return_data['created_date'] = $curr_date;
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy;
		// $drawing_object->width =  $width;
		// $drawing_object->height =  $height;
		$drawing_object->width =  $newValWidth;
		$drawing_object->height =  $newValHeight;
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id;
		$drawing_object->image_url = $imagepath;
		$drawing_object->type = "image";
		$drawing_object->object_type = "image";
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		$saved_file_h["page_id"] 	= $page_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		
		//print_r($saved_file_h);
		// $this->file_model->save_file_history($saved_file_h);
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		 // echo json_encode($return_data);
		echo json_encode(array('status'=>true,'message'=>json_encode($return_data),'history'=>$saved_file_h));
	}	
	
	public function add_uploaded_file()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$imagepath = $this->input->post('imagepath');
		$path = $this->input->post('filepath');
		$path_file = $this->input->post('filepath_file');
		$object_id =$this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");
		$content_file =$this->input->post("content_file");
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
		
		$sizes = getimagesize($path);

		$width = $sizes[0];
		$height = $sizes[1];
		copy( $path , $imagepath );
		copy( $path_file , $content_file );
		
		$return_data['path'] = $imagepath;
		$return_data['w'] = $width;
		$return_data['h'] = $height;
		$return_data['x'] = $posx;
		$return_data['y'] = $posy;
		
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy;
		$drawing_object->width =  $width;
		$drawing_object->height =  $height;
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id;
		$drawing_object->image_url = $imagepath;
		$drawing_object->type = "attach";
		$drawing_object->object_type = "attach";
		$drawing_object->content_file = $content_file;
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["page_id"] 	= $page_id;
		$saved_file_h["changed_object_id"] 	= $object_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		
		//print_r($saved_file_h);
		// $this->file_model->save_file_history($saved_file_h);
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		// echo json_encode($return_data);
		// echo $drawing_object;
		echo json_encode(array('status'=>true,'message'=>$drawing_object,'history'=>$saved_file_h));
		
	}
	
	public function add_chart_fill()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id"); 
		$object_id =$this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy"); 
		$data_chart =$this->input->post("data_chart");
		$type_chart =$this->input->post("type_chart");
		$width_chart =$this->input->post("width_chart");
		$height_chart =$this->input->post("height_chart");
		$fill_color =$this->input->post("fill_color");
		$gradient =$this->input->post("gradient");
		$axisCrd =$this->input->post("axisCrd");
		$start_color =$this->input->post("start_color");
		$end_color =$this->input->post("end_color");
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
		// print_r($fill_color);
		$this->chart_uploader(); 
        $ydata = $data_chart;  //array(11,3,8,12,5,1,9,13,5,7); // this should come from the model   
        $graph = $this->jpgraph_pi->chartgraph($ydata, '', $type_chart, $width_chart, $height_chart, $axisCrd, $fill_color, $gradient , $start_color, $end_color); 
		
	
		
        // File locations
        // Could possibly add to config file if necessary
        $graph_temp_directory = 'documents';  // in the webroot (add directory to .htaccess exclude)
        $graph_file_name = $_POST['id'].'.png'; //$object_id.'.png';// 
        $graph_file_location = $graph_temp_directory . '/' . $graph_file_name;
                
        
		
		$documents_chart = 'documents/'.$file_id.'/'.$graph_file_name;
		
		$graph->Stroke('./'.$documents_chart);  // create the graph and write to file
        
        $data['graph'] = $documents_chart; 
		// echo $documents_chart;
		// echo 'abcd';
		// copy( $graph_file_location , $documents_chart ); 
		
		$return_data['width'] = $width_chart;
		$return_data['height'] = $height_chart;
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $object_id;
		$return_data['type'] = "chart";
		$return_data['object_type'] = "chart";
		$return_data['type_chart'] = $type_chart;
		$return_data['image_url'] = $documents_chart;
		// $return_data['image_url'] = $documents_chart;
		$return_data['data_chart'] = $data_chart;
		$return_data['created_by'] = $username;
		$return_data['created_date'] = $curr_date;
		$return_data['color1'] = $fill_color;
		$return_data['color2'] = $start_color;
		$return_data['color3'] = $end_color;
		$return_data['scale'] = $axisCrd;
		$return_data['gradient'] = $gradient;
		
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		 
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy;
		$drawing_object->width =  $width_chart;
		$drawing_object->height =  $height_chart;
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id;
		// $drawing_object->image_url = $graph_file_location;
		$drawing_object->image_url = $documents_chart;
		$drawing_object->type = "chart";
		$drawing_object->object_type = "chart";
		$drawing_object->type_chart = $type_chart;
		$drawing_object->data_chart = implode(",", $data_chart);
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
		$drawing_object->color1 = $fill_color;
		$drawing_object->color2 = $start_color;
		$drawing_object->color3 = $end_color;
		$drawing_object->scale = $axisCrd;
		$drawing_object->gradient = $gradient;
		//$drawing_object->content_file = $content_file;
		// echo $drawing_object->data_chart;
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		// $return_data = json_encode($return_data);
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["page_id"] 	= $page_id;
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		
		//print_r($saved_file_h);
		// $this->file_model->save_file_history($saved_file_h);
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		// echo json_encode($return_data);
		echo json_encode(array('status'=>true,'message'=>json_encode($return_data),'history'=>$saved_file_h));
	}
	
	public function get_file_data()
	{
		$file_id=$this->input->post('file_id');
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file->last_update=time();
		echo json_encode($file);
	}
	
	public function get_data_page()
	{
		$file_id=$this->input->post('file_id');
		$page_id=$this->input->post('page_id');
		
		$file = $this->file_model->get_file_by_file_id($file_id);
		$json_object = json_decode($file->content); 
		
		$new_content = null;
		$page_drawing_object=null;
		$changed_drawing_object=null;
		$update_time = time(); 
		
		foreach($json_object->pages as $page_key => $single_page)
		{ 
			if($single_page->id == $page_id)
			{	
				$page_drawing_object = $single_page;
				break;
			}
		} 
		
		$new_content = json_encode($page_drawing_object);
		echo $new_content;
	}
	
	public function add_chat_history()
	{
		
		// $saved_file["content"] = $json_encoded;
		// $this->file_model->save($saved_file, $file_id);
		
		$saved_file_h["file_id"] 	= $this->input->post("file_id");
		$saved_file_h["user_id"] 	= $this->input->post("user_id");
		$saved_file_h["username"] 	= $this->input->post("username");
		$saved_file_h["message"] 	= $this->input->post("message");
		
		$this->file_model->save_chat_history($saved_file_h);
	}
	
	public function add_text()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$object_id = $this->input->post("object_id");
		$content = $this->input->post('content');
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");
		$w =$this->input->post("width");
		$h =$this->input->post("height");
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
	
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy;
		$drawing_object->width =  $w;
		$drawing_object->height =  $h;
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id;
		$drawing_object->content = $content;
		$drawing_object->type = $this->input->post("kind");
		$drawing_object->object_type = $this->input->post("kind");
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
		$drawing_object->kind = $this->input->post("type");
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["page_id"] 	= $page_id;
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		
		$return_data=array();
		$return_data['id']=$object_id;
		$return_data['width']=$w;
		$return_data['height']=$h;
		$return_data['posx']=$posx;
		$return_data['posy']=$posy;
		$return_data['content']=$content;	
		$return_data['object_type']=$this->input->post("kind");	
		$return_data['created_by'] = $username;
		$return_data['created_date'] = $curr_date;
		$return_data['object_kind'] = $this->input->post("type");
		
		// $this->file_model->save_file_history($saved_file_h);
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		// echo json_encode($return_data);
		echo json_encode(array('status'=>true,'message'=> json_encode($return_data),'history'=>$saved_file_h));
	}
	
	public function add_sticky_text()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$object_id = $this->input->post("object_id");
		$content = $this->input->post('content');
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");
		$w =$this->input->post("width");
		$h =$this->input->post("height");
		$background =$this->input->post("background");
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
	
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy;
		$drawing_object->width =  $w;
		$drawing_object->height =  $h;
		$drawing_object->background =  $background;
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id;
		$drawing_object->content = $content;
		$drawing_object->type = "sticky-text";
		$drawing_object->object_type = "sticky-text";
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
		
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["page_id"] 	= $page_id;
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		
		$return_data=array();
		$return_data['id']=$object_id;
		$return_data['width']=$w;
		$return_data['height']=$h;
		$return_data['posx']=$posx;
		$return_data['posy']=$posy;
		$return_data['content']=$content;
		$return_data["background"] 	= $this->input->post("background");
		$return_data['created_by'] = $username;
		$return_data['created_date'] = $curr_date;
		$return_data['object_type'] = "sticky-text";
		// $this->file_model->save_file_history($saved_file_h);
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		// echo json_encode($return_data);
		echo json_encode(array('status'=>true,'message'=> json_encode($return_data),'history'=>$saved_file_h));
	}
	
	public function add_page_object()
	{
		$file_id = $this->input->post("file_id");
		$width = $this->input->post("width");
		$height = $this->input->post("height");
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
		$json_decoded = json_decode($file->content);
		
		$size_pages = count($json_decoded->pages);
		// echo $json_decoded->pages[$size_pages - 1]->id;
		$id_page = $json_decoded->pages[$size_pages - 1]->id;
		
		$page_draw = new stdClass();
		$page_draw->id = $id_page + 1;
		$page_draw->last_update = time();
		$page_draw->width = $width;
		$page_draw->height = $height;
		
		copy( 'documents/thumb-blank.png' ,'documents/'.$file_id.'/thumb-'.$page_draw->id.'.png'  );
		copy( 'documents/background-blank.png' ,'documents/'.$file_id.'/background-'.$page_draw->id.'.png'  );
		$page_draw->drawing_objects = array();
		
		array_push($json_decoded->pages,$page_draw);
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$page_draw = json_encode($page_draw);
		echo $page_draw; 
	}
	
	public function add_file_page_object()
	{
		$file_id = $this->file_model->get_max_id()->MAX;
		$file_id = $file_id + 1;
		
		$file_id_curr = $this->input->post("file_id");
		$file_name = $this->input->post("file_name");
		$width = $this->input->post("width");
		$height = $this->input->post("height");
		$group_id = $this->input->post("group_id");
		
		$file_curr = $this->file_model->get_file_by_file_id($file_id_curr);
		
		mkdir('documents/'.$file_id);
		
		copy( 'documents/thumb-blank.png' ,'documents/'.$file_id.'/thumb-1.png'  );
		copy( 'documents/background-blank.png' ,'documents/'.$file_id.'/background-1.png'  );
		$file_page = new stdClass();
		$file_page->pages = array();
		$page_draw = new stdClass();
		$page_draw->id = 1;
		$page_draw->last_update = time();
		$page_draw->width = $width;
		$page_draw->height = $height;
		$page_draw->drawing_objects = array();
		
		array_push($file_page->pages,$page_draw);
		
		$json_encoded = json_encode($file_page);
		
		$saved_file["file_id"] 				= $file_id;
		if($group_id <= 0)
			$saved_file["group_id"] 		= $file_curr->group_id;//1;
		else
			$saved_file["group_id"] 		= $group_id;
			
		$saved_file["file_name"] 			= $file_name;
		$saved_file["content"] 				= $json_encoded;
		$saved_file["last_update"] 			= time();
		$saved_file["created_on"] 			= time();
		
		$this->file_model->save_file($saved_file); 
		
		 
		echo $file_id;
		
	}
	public function get_recover_list()
	{
		$file_id = $this->input->get("file_id");
		$page_id = $this->input->get("page_id");
		$limit = $this->input->get("time_limit");
		$recover_list = $this->file_model->get_recover_list(0,$file_id,$page_id);
		$valid_recover_list = array();
		foreach($recover_list as $recover_list_item)
		{
			if(!$this->file_model->is_already_recovered($recover_list_item->history_id))
			{
				array_push($valid_recover_list,$recover_list_item);
			}
		}
		echo json_encode($valid_recover_list);
	}
	public function set_page_background()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$image_path = $this->input->post('image_path');
		if($image_path ==""){
			copy( "documents/background-blank.png" ,'documents/'.$file_id.'/background-'.$page_id.'.png'  );
			$image_path = "documents/background-blank.png";
		}
		else
			copy( $image_path ,'documents/'.$file_id.'/background-'.$page_id.'.png'  );
		$json = new stdClass();
		$json->image_path = $image_path;
		$json->type = "background";
		$json->object_type = "background";
		
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "EDIT";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= json_encode($json);
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= -1;
		 $user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		$saved_file_h["page_id"] 	= $page_id;
		
		$this->file_model->save_file_history($saved_file_h);
	}
	public function add_thumb_canvas()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		// $object_id = $this->input->post("object_id");
		$path = $this->input->post('filepath');
		$image_data = $this->input->post('image_data');
		$image_data = explode('base64,',$image_data); 
		$size = file_put_contents($path, base64_decode($image_data[1]));
		
		echo $size;
	}
	
	public function text_to_image()
	{ 
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$object_id = $this->input->post("id");
		$posx = $this->input->post("posx");
		$posy = $this->input->post("posy");
		$username = $this->input->post("username");
		$curr_date = $this->input->post("curr_date");
		$string = $this->input->post("text_input"); 
		$path = $this->input->post('image_path');  
		$colorText1 = $this->input->post('colorText1'); 
		$colorText2 = $this->input->post('colorText2'); 
		$textBackground1 = $this->input->post('textBackground1'); 
		$colorBackground2 = $this->input->post('colorBackground2'); 
		$colorShadow = $this->input->post('colorShadow');  	
		$checkTransChar = $this->input->post('checkTransChar');  
		$isBold = $this->input->post('isBold');  
		$isItalic = $this->input->post('isItalic');  
		$isUnderline = $this->input->post('isUnderline');   
		$fontStyle = $this->input->post('fontStyle');   
		$fontSize = $this->input->post('fontSize');   
		$checkGradChar = $this->input->post('checkGradChar');   
		$checkGradBack = $this->input->post('checkGradBack');  
		$valGradChar = $this->input->post('valGradChar');  
		$valGradBack = $this->input->post('valGradBack');   
		$widthDiv = $this->input->post('widthDiv');   
		$heightDiv = $this->input->post('heightDiv');   	
		$checkVert = $this->input->post('checkVert'); 
		$chooseSad = $this->input->post('chooseSad'); 
		$chooseWidthShad = $this->input->post('chooseWidthShad');  
		
		$fontStyleCompleted = null;
		$valFontUnder = null;
		
		header("Content-type: image/png");
     
		$font  = 520; 
		$width  = $widthDiv; 
		$height = $heightDiv ;
		
		// print_r($chooseSad);
		// $width  = 150; 
		// $height = 150;
		
		$fontSize = $fontSize + 2;
		// $fontSize = 48;
		$fontSizeWidth = $fontSize ;
		
		$heightPos = ($fontSize * 2) - 18;
		$heightPos2 = $heightPos + 5;
		
		$widthPos = 3;
		 
		
		$fontStyleCompleted = $fontStyle;
		
		if($isBold == 1)
		{
			$fontStyleCompleted = $fontStyleCompleted.'-Bold';
		}
		
		if($isItalic == 1)
		{
			$fontStyleCompleted = $fontStyleCompleted.'-Italic';
		}
		
		if($isUnderline == 1)
		{
			$valFontUnder = "decorate Underline"; 
		}
		else
		{
			$valFontUnder = "";
		}
		
		// if($checkGradBack == 1)
		// {
			// $heightNew = $height * 4; 
			 
			// if($valGradBack == "l2r"){
				// $cmd = 'convert -size '.$width.'x'.$heightNew.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate -90 -gravity center -crop '.$width.'x'.$height.'+0+0 +repage ';		
			// }else if($valGradBack == "t2b"){
				// $cmd = 'convert -size '.$width.'x'.$heightNew.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate 0 -gravity center -crop '.$width.'x'.$height.'+0+0 +repage ';
			// }else if($valGradBack == "topL"){
				// $cmd = 'convert -size '.$width.'x'.$heightNew.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate -45 -gravity center -crop '.$width.'x'.$height.'+0+0 +repage ';
			// }else if($valGradBack == "upperR"){ 
				// $cmd = 'convert -size '.$width.'x'.$heightNew.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate 45 -gravity center -crop '.$width.'x'.$height.'+0+0 +repage ';
			// }
			// $fontSizeWidth = $fontSizeWidth - 10;
			 
				
			// $cmd	= $cmd.' -font '.$fontStyleCompleted.' -pointsize '.$fontSize.' -tile gradient:'.$colorText1.'-'.$colorText2.' '.
				// '-draw "font '.$fontStyleCompleted.' font-size '.$fontSize.' '.$valFontUnder.' translate 0,0 rotate 0 text 0,'.$fontSizeWidth.' \"'.$string.'\""  documents/'.$file_id.'/'.$object_id.'.png'; 
		// }
		// else
		// {
			// $cmd =	'convert '. 
					// '-size '.$width.'x'.$height.' '.
					// 'gradient:'.$textBackground1.'-'.$colorBackground2.' ';
			
			// $cmd	= $cmd.' -font '.$fontStyleCompleted.' -pointsize '.$fontSize.' -tile gradient:'.$colorText1.'-'.$colorText2.' '.
				// '-draw "font '.$fontStyleCompleted.' font-size '.$fontSize.' '.$valFontUnder.' translate 0,0 rotate 0 text 0,'.$fontSizeWidth.' \"'.$string.'\""  documents/'.$file_id.'/'.$object_id.'.png'; 
		// }
		
		if($checkGradBack == 1)
		{
			$heightNew = $height * 4; 
			// $heightNew = $height * 54; 
			
			$cmd = 	'convert -size '.$width.'x'.$height.' gradient:'.$textBackground1.'-'.$colorBackground2.' documents/'.$file_id.'/'.$object_id.'-oB.png '; 
			exec($cmd);
			$cmd= "";
			
			if($width > $height)
			{ 
				if($valGradBack == "l2r"){
					$cmd = 'convert -size '.$width.'x'.$width.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate 0  documents/'.$file_id.'/'.$object_id.'-rB.png '; 
					exec($cmd);
					$cmd= "";	
				}else if($valGradBack == "t2b"){
					// $cmd = 'convert -size '.$width.'x'.$width.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rB.png '; 
					// exec($cmd);
					// $cmd= "";
				}else if($valGradBack == "topL"){
					$cmd = 'convert -size '.$width.'x'.$width.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate -90  documents/'.$file_id.'/'.$object_id.'-rB.png '; 
					exec($cmd);
					$cmd= "";
				}else if($valGradBack == "upperR"){ 
					$cmd = 'convert -size '.$width.'x'.$width.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rB.png '; 
					exec($cmd);
					$cmd= "";
				}
			}else{
				 if($valGradBack == "l2r"){
					$cmd = 'convert -size '.$height.'x'.$height.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate 0  documents/'.$file_id.'/'.$object_id.'-rB.png '; 
					exec($cmd);
					$cmd= "";	
				}else if($valGradBack == "t2b"){
					// $cmd = 'convert -size '.$height.'x'.$height.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rB.png '; 
					// exec($cmd);
					// $cmd= "";
				}else if($valGradBack == "topL"){
					$cmd = 'convert -size '.$height.'x'.$height.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate -90  documents/'.$file_id.'/'.$object_id.'-rB.png '; 
					exec($cmd);
					$cmd= "";
				}else if($valGradBack == "upperR"){ 
					$cmd = 'convert -size '.$height.'x'.$height.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rB.png '; 
					exec($cmd);
					$cmd= "";
				}
			}
			
			$cmd = 'composite documents/'.$file_id.'/'.$object_id.'-oB.png   -compose Multiply documents/'.$file_id.'/'.$object_id.'-rB.png  documents/'.$file_id.'/'.$object_id.'-resB.png '; 
			exec($cmd);
			$cmd= "";
			
			$cmd = 'convert documents/'.$file_id.'/'.$object_id.'-resB.png  -gravity center -crop '.$width.'x'.$height.'+0+0 +repage documents/'.$file_id.'/'.$object_id.'-resB2.png '; 
			exec($cmd);
			$cmd= "";
			
			// $cmd = 'convert -size '.$width.'x'.$heightNew.' gradient:'.$textBackground1.'-'.$colorBackground2.' -rotate -45 -gravity center -crop '.$width.'x'.$height.'+0+0 +repage documents/'.$file_id.'/'.$object_id.'-resB.png ';
			// print_r($cmd);
			// exec($cmd);
			// $cmd= "";
			
		}
		else if($checkGradBack == 0)
		{
			$cmd = 'convert '.$width.'x'.$height.' gradient:'.$textBackground1.'-'.$textBackground1.' documents/'.$file_id.'/'.$object_id.'-resB2.png ';
					// convert -size 150x150 gradient:yellow-red -rotate 90  D:/tes/gradient_dst.png 
					// composite D:/tes/gradient_src.png  -compose Multiply D:/tes/gradient_dst.png D:/tes/gradient_result.png ';
			// print_r($cmd);
			exec($cmd);
			$cmd= "";
		}
		
		if($checkGradChar == 1)
		{
			$cmd =	'convert -size '.$width.'x'.$height.' gradient:'.$colorText1.'-'.$colorText2.' documents/'.$file_id.'/'.$object_id.'-oC.png ';
			exec($cmd);
			$cmd= "";
			
			if($width > $height)
			{
				// $cmd =	'convert -size '.$width.'x'.$width.' gradient:'.$colorText1.'-'.$colorText2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rC.png ';
				// exec($cmd);
				// $cmd= "";
				if($valGradBack == "l2r"){
					$cmd =	'convert -size '.$width.'x'.$width.' gradient:'.$colorText1.'-'.$colorText2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rC.png ';
					exec($cmd);
					$cmd= "";	
				}else if($valGradBack == "t2b"){
					// $cmd =	'convert -size '.$width.'x'.$width.' gradient:'.$colorText1.'-'.$colorText2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rC.png ';
					// exec($cmd);
					// $cmd= "";
				}else if($valGradBack == "topL"){
					$cmd =	'convert -size '.$width.'x'.$width.' gradient:'.$colorText1.'-'.$colorText2.' -rotate -90  documents/'.$file_id.'/'.$object_id.'-rC.png ';
					exec($cmd);
					$cmd= "";
				}else if($valGradBack == "upperR"){ 
					$cmd =	'convert -size '.$width.'x'.$width.' gradient:'.$colorText1.'-'.$colorText2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rC.png ';
					exec($cmd);
					$cmd= "";
				}
			}else{
				// $cmd =	'convert -size '.$height.'x'.$height.' gradient:'.$colorText1.'-'.$colorText2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rC.png ';
				// exec($cmd);
				// $cmd= "";
				if($valGradBack == "l2r"){
					$cmd =	'convert -size '.$height.'x'.$height.' gradient:'.$colorText1.'-'.$colorText2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rC.png ';
					exec($cmd);
					$cmd= "";	
				}else if($valGradBack == "t2b"){
					// $cmd =	'convert -size '.$height.'x'.$height.' gradient:'.$colorText1.'-'.$colorText2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rC.png ';
					// exec($cmd);
					// $cmd= "";
				}else if($valGradBack == "topL"){
					$cmd =	'convert -size '.$height.'x'.$height.' gradient:'.$colorText1.'-'.$colorText2.' -rotate -90  documents/'.$file_id.'/'.$object_id.'-rC.png ';
					exec($cmd);
					$cmd= "";
				}else if($valGradBack == "upperR"){ 
					$cmd =	'convert -size '.$height.'x'.$height.' gradient:'.$colorText1.'-'.$colorText2.' -rotate 90  documents/'.$file_id.'/'.$object_id.'-rC.png ';
					exec($cmd);
					$cmd= "";
				}
				// print_r($cmd);
			}
			
			$cmd =	'composite documents/'.$file_id.'/'.$object_id.'-oC.png  -compose Multiply documents/'.$file_id.'/'.$object_id.'-rC.png documents/'.$file_id.'/'.$object_id.'-resC.png '; 
			exec($cmd);
			$cmd= "";
			
			$cmd = 'convert documents/'.$file_id.'/'.$object_id.'-resC.png  -gravity center -crop '.$width.'x'.$height.'+0+0 +repage documents/'.$file_id.'/'.$object_id.'-resC2.png '; 
			exec($cmd);
			$cmd= "";
		}
		else if($checkGradChar == 0)
		{
			$cmd = 'convert -size '.$width.'x'.$height.' gradient:'.$colorText1.'-'.$colorText1.' documents/'.$file_id.'/'.$object_id.'-resC2.png ';
					// convert -size 150x150 gradient:yellow-red -rotate 90  D:/tes/gradient_dst.png 
					// composite D:/tes/gradient_src.png  -compose Multiply D:/tes/gradient_dst.png D:/tes/gradient_result.png ';
			// print_r($cmd);
			exec($cmd);
			$cmd= "";
		}
		// print_r($chooseSad);
		$fontSizeShad = null;
		if($chooseSad <> "noShadow")
		{
			if($chooseSad == "dropShadow")
			{	
				$heightPos = $heightPos2 + $chooseWidthShad;
				$widthPos = $widthPos + $chooseWidthShad;
			}
			// else if($chooseSad == "blockShadow")
			// {
				// $heightPos = $heightPos2 + $chooseWidthShad;
				// $widthPos = $widthPos + $chooseWidthShad;
				// $fontSizeShad = $fontSize + $chooseWidthShad;
			// }
			// else if($chooseSad == "outlineBlock")
			// {
			// }
			else if($chooseSad == "floating")
			{
			}
			else if($chooseSad == "depression")
			{
			}
		}
		else
		{
			$colorBackground2 = "";
		}
		$cmd = 'convert -compose Dst_In ( -size '.$width.'x'.$height.' gradient:'.$colorBackground2.'-'.$colorBackground2.'  ) ( -size '.$width.'x'.$height.' null: -font '.$fontStyleCompleted.' -pointsize '.$fontSize.' ';
		// print_r($chooseSad);
		if($chooseSad == "blockShadow")
		{
			$countWidth = 1;
			$widthPosShadTemp = $widthPos;
			$heightPosShadTemp = $heightPos;
			$chooseWidthShad = $chooseWidthShad *2;
			while($countWidth < $chooseWidthShad)
			{
				$cmd = $cmd.'-draw "font '.$fontStyleCompleted.' font-size '.$fontSize.' '.$valFontUnder.'  translate 0,0 rotate 0 text '.$widthPosShadTemp.','.$heightPosShadTemp.' ';
				
				$widthPosShadTemp = $widthPosShadTemp + 1;
				$heightPosShadTemp = $heightPosShadTemp + 1;
				$countWidth = $countWidth + 1;
				// print_r("ke- ".$widthPosShadTemp);
				// print_r("coin ".$countWidth);
			}
			$cmd = $cmd.'\"'.$string.'\"" ) -composite documents/'.$file_id.'/'.$object_id.'-text.png ';
		}
		else
		{
			$cmd = $cmd.'-draw "font '.$fontStyleCompleted.' font-size '.$fontSize.' '.$valFontUnder.'  translate 0,0 rotate 0 text '.$widthPos.','.$heightPos.' \"'.$string.'\"" ) -composite documents/'.$file_id.'/'.$object_id.'-text.png ';			
		}

		// print_r($cmd); 		
		exec($cmd);
		$cmd= "";
		
		$cmd = 'convert -compose Dst_In ( documents/'.$file_id.'/'.$object_id.'-resC2.png  ) ( -size '.$width.'x'.$height.' null: -font '.$fontStyleCompleted.' -pointsize '.$fontSize.' -draw "font '.$fontStyleCompleted.' font-size '.$fontSize.' '.$valFontUnder.'  translate 0,0 rotate 0 text 3,'.$heightPos2.' \"'.$string.'\"" ) -composite documents/'.$file_id.'/'.$object_id.'-comb.png ';
		// print_r($cmd);
		exec($cmd);
		$cmd= "";
		
		$cmd = 'composite -compose Screen documents/'.$file_id.'/'.$object_id.'-text.png  documents/'.$file_id.'/'.$object_id.'-comb.png  documents/'.$file_id.'/'.$object_id.'-result1.png ';
		// print_r($cmd);
		exec($cmd);
		$cmd= "";
		
		$cmd = 'composite -compose Screen documents/'.$file_id.'/'.$object_id.'-resB2.png  documents/'.$file_id.'/'.$object_id.'-result1.png  documents/'.$file_id.'/'.$object_id.'.png ';
		// print_r($cmd);
		exec($cmd);
		$cmd= "";
		
		// print_r($cmd);
		
		//Execution Requirement
		// exec($cmd);
		
		// Rotation Image
		// ==============
		if($checkVert == 1){
			$cmd_rotate_img = 	'convert '.
								'documents/'.$file_id.'/'.$object_id.'.png '.
								'-rotate 90 ';
		}else{
			$cmd_rotate_img = 	'convert '.
								'documents/'.$file_id.'/'.$object_id.'.png '.
								'-rotate 0 ';
		}
		if($checkTransChar == 1){
			$cmd_rotate_img = $cmd_rotate_img.'-channel Alpha -evaluate Divide 3 ';
		}
		$cmd_rotate_img = $cmd_rotate_img.'documents/'.$file_id.'/'.$object_id.'.png';
		//Execute Rotation Image
		exec($cmd_rotate_img);
			
			
			
			// print_r($width);
			  // exec('convert -size 81x80 gradient:'.$textBackground1.'-'.$colorBackground2.' -font Arial -pointsize 80 Center decorate Underline Bold -tile // gradient:'.$colorText1.'-'.$colorText2.' -annotate +0+76 "'.$string.'" documents/'.$file_id.'/'.$object_id.'.png');		  
			  // exec('convert -size '.$width.'x120 gradient:'.$textBackground1.'-'.$colorBackground2.' -draw "font Candice  font-size 72  decorate UnderLine fill-gradient '.$colorText1.'-'.$colorText2.'stroke navy  stroke-width 2 translate 10,110 rotate -45 text 0,0 '.$string.'"  documents/'.$file_id.'/'.$object_id.'.png');
			  
			// print_r($cmd);
			// $cmd =	'convert '.
					// '-size 640x120 '.
					// 'gradient:"yellow-green" '.
					// '-font Arial -pointsize 60 -tile gradient:'.$colorText1.'-'.$colorText2.' -annotate 18 "Nadna<br>Pixels" '.
					// 'pango:"Nanda<br>" documents/'.$file_id.'/'.$object_id.'.png';
			
			// $cmd =	'convert '.
					// ' -background lightblue '.
					// 'pango:"  Some  <span size="50000" rise="-10000" foreground="red" background="blue" > Big Red on Blue </span>  Text  " '.  
					// 'gradient:"yellow-green" '.
					// '-font Arial -pointsize 60 -tile gradient:'.$colorText1.'-'.$colorText2.' -annotate 18 "Nadna<br>Pixels" '.
					// 'documents/'.$file_id.'/'.$object_id.'.png';
			// exec($cmd);
			
		  // exec('convert -size 1x1 gradient:yellow-blue -font Arial -pointsize 70 -tile gradient:blue-red -draw "font Arial  font-size 70 decorate UnderLine translate 10,110 rotate 0 text 0,10 \"'.$string.'\"" D:/21.png');
		  // exec('convert D:/21.png -rotate 90 D:/21.png');
		  // echo "Nanda";
		  // exec('convert -background lightblue -fill blue -font Candice -pointsize 48 label:'ab%cde' D:/label_percent.gif');
 
		  // $im = @imagecreatetruecolor ($width,$height);
 
		  // $background_color = imagecolorallocate ($im, 255, 255, 255);
		  // $background_color = imagefilledrectangle($im, 0, 0, 299, 99, $red);
 
		  // $text_color = imagecolorallocate ($im, 255, 255,255);
 
		  // imagestring ($im, $font, 5, 5,  $string, $text_color);
 
		  // imagepng ($im);
		  // ImagePNG($im,$path);
			// ImageDestroy($im);
		
		$return_data['width'] = $width;
		$return_data['height'] = $height;
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $object_id;
		$return_data['type'] = "image";
		$return_data['image_url'] = $path;
		$return_data['object_type'] = "image";
		$return_data['created_by'] = $username;
		$return_data['created_date'] = $curr_date; 
		
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->width = $width;
		$drawing_object->height = $height;
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy; 
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id; 
		$drawing_object->type = "image";
		$drawing_object->object_type = "image";
		$drawing_object->image_url = $path;
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date; 
		
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object); 
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		 $user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		$saved_file_h["page_id"] 	= $page_id;
		
		$this->file_model->save_file_history($saved_file_h);
		echo json_encode(array('status'=>true,'message'=> json_encode($return_data),'history'=>$saved_file_h));
		 // echo json_encode($return_data);
	}
	
	public function add_object()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$object_id = $this->input->post("object_id");
		$image_data = $this->input->post('image_data');
		$path = $this->input->post('filepath');
		$object_id = $this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");
		$obj_type =$this->input->post("obj_type");
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
		$image_data = explode('base64,',$image_data); 
		$size = file_put_contents($path, base64_decode($image_data[1]));
	
		$sizes = getimagesize($path);
	
		$return_data['width'] = $sizes[0];
		$return_data['height'] = $sizes[1];
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $object_id;
		$return_data['type'] = "image";
		$return_data['image_url'] = $path;
		$return_data['created_by'] = $username;
		$return_data['created_date'] = $curr_date;
		
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy;
		$drawing_object->width =  $sizes[0];
		$drawing_object->height =  $sizes[1];
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id;
		$drawing_object->image_url = $path;
		$drawing_object->type = "image";
		$drawing_object->object_type = $obj_type;
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["page_id"] 	= $page_id;
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;
		
		//print_r($saved_file_h);
		// $this->file_model->save_file_history($saved_file_h);
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		// echo json_encode($return_data);
		echo json_encode(array('status'=>true,'message'=> json_encode($return_data),'history'=>$saved_file_h));
		
	}
	
	public function poll_new_data()
	{
		$last_update_time=$this->input->post('last_update_time');
		
		$page_id = $this->input->post("page_id");
		$file_id = $this->input->post("file_id");
		
		
		$return_data["content"] = $this->file_model->get_file_history_since($last_update_time,$file_id);
		$current_time =  time();
		$file = $this->file_model->get_file_by_file_id($file_id);
		
		if(empty($return_data["content"]))
		{
			$return_data["last_update"] = $last_update_time;
		}
		else
			$return_data["last_update"] = $current_time;
		echo json_encode($return_data);
		
	}
	
	public function poll_chat_history()
	{

		$file_id = $this->input->post("file_id");
		
		
		$return_data["content"] = $this->file_model->get_chat_history_on_file($file_id);
		echo json_encode($return_data);
		
	}
	
	public function delete_object()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id");
		$drawing_object_id = $this->input->post("id");
		
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
		
		$delete_object_index_array = -1;
		$delete_object_drawing_index_array = -1;
		$deleted_object="";
		$content_file = json_decode($file->content); 
		
		foreach($content_file->pages as $page_key => $single_page)
		{
			if($single_page->id == $page_id)
			{
				$delete_object_index_array = $page_key;
				foreach($single_page->drawing_objects as $drawing_key => $single_drawing_object)
				{
					if($single_drawing_object->id == $drawing_object_id)
					{
						$delete_object_drawing_index_array = $drawing_key;
						$deleted_object = $single_drawing_object;
						break;
					}
				}
			}
		}
		
		if($delete_object_drawing_index_array > -1){
			$json_encoded = json_encode($content_file);
			// print_r($json_encoded);
			// print_r($content_file->pages[$delete_object_index_array]->drawing_objects);
			unset($content_file->pages[$delete_object_index_array]->drawing_objects[$delete_object_drawing_index_array]);
			$content_file->pages[$delete_object_index_array]->drawing_objects = array_values($content_file->pages[$delete_object_index_array]->drawing_objects);
			$json_encoded = json_encode($content_file);
			// print_r($content_file->pages[$delete_object_index_array]->drawing_objects);
			
			// print_r($json_encoded);
			$saved_file["content"] = $json_encoded;
			$this->file_model->save($saved_file, $file_id);
			
			$single_drawing_object = json_encode($deleted_object);
			
			// print_r($single_drawing_object);
			// print_r($page_id);
			//$files_fil = new Files();
			//$results = $files->delete
			
			//commented By Firman
			//unlink('documents/'.$file_id.'/'.$drawing_object_id.'.png');
			
			// print_r('documents/'.$file_id.'/'.$drawing_object_id.'.png');
			
			$saved_file_h["file_id"] 			= $file_id;
			$saved_file_h["operation"] 			= "DEL";
			$saved_file_h["updated_time"] 		= time();
			$saved_file_h["changed_content"] 	= "";
			$saved_file_h["previous_content"] 	= $single_drawing_object;
			$saved_file_h["changed_object_id"] 	= $drawing_object_id;
			$user = $this->ion_auth->user()->row();
			$saved_file_h["user_id"] 	= $user->id;
			$saved_file_h["page_id"] 	= $page_id;

			//print_r($single_drawing_object);
			$h_id = $this->file_model->save_file_history($saved_file_h);
			$saved_file_h["history_id"] = $h_id;
			echo json_encode(array('status'=>true,'message'=> "",'history'=>$saved_file_h));
		}else{
			echo 'Id not mathed!.';
		}
		//print_r($content_file);
		
		
	}
	public function get_history()
	{	
		$file_id = $this->input->get("f_id");
		$page_id = $this->input->get("p_id");
		if(empty($file_id) || empty($page_id))
		{
			echo "empty page/file id";
			return;
		}
		$this->load->model('file_model');
		$this->load->model('user_model');
		
		$file_data =$this->file_model->get_file_by_file_id($file_id);
		$file_history =$this->file_model->get_file_history_since(0,$file_id,$page_id);
		foreach($file_history as $single_history)
		{
			if($single_history->operation=="ADD")
				$single_history->previous_content  = json_decode($single_history->changed_content );
			else
				$single_history->previous_content = json_decode($single_history->previous_content);
			
			if($single_history->operation!="DEL")
			{
				$single_history->changed_content  = json_decode($single_history->changed_content );
				$single_history->previous_content  = $single_history->changed_content ;
			}
			//Nanda - Notes: Change attribute object_type to attribute type
			$single_history->type = $single_history->previous_content->type;
			// $single_history->object_type = $single_history->previous_content->object_type;
			$single_history->user = $this->user_model->get_user_by_id($single_history->user_id);
			
			 
		}
		echo json_encode($file_history);
	}
	public function store_image()
	{
		$image_data = $this->input->post('image_data');
		$path = $this->input->post('filepath');
		
		$image_data = explode('base64,',$image_data); 
		
		file_put_contents($path, base64_decode($image_data[1]));
		$sizes = getimagesize($path);
		$return_data['path'] = $path;
		$return_data['w'] = $sizes[0];
		$return_data['h'] = $sizes[1];
		
		echo json_encode($return_data);
	}
	
	public function add_table()
	{
		$file_id = $this->input->post("file_id");
		$page_id = $this->input->post("page_id"); 
		$object_id =$this->input->post("id");
		$posx =$this->input->post("posx");
		$posy =$this->input->post("posy");	
		$width =$this->input->post("width");
		$height =$this->input->post("height");
		$content_table =$this->input->post("content_table");
		$username =$this->input->post("username");
		$curr_date =$this->input->post("curr_date");
		$border_style =$this->input->post("border_style");
		$row_table =$this->input->post("row_table");
		$col_table =$this->input->post("col_table");
		$style =$this->input->post("style");
		$thickness =$this->input->post("thickness");
		
		$return_data['width'] = $width;
		$return_data['height'] = $height;
		$return_data['posx'] = $posx;
		$return_data['posy'] = $posy;
		$return_data['id'] = $object_id;
		$return_data['type'] = "table";
		$return_data['style'] = $style;
		$return_data['thickness'] = $thickness;
		$return_data['content_table'] = $content_table;
		$return_data['object_type'] = "table";
		$return_data['created_by'] = $username;
		$return_data['created_date'] = $curr_date;
		$return_data['border_style'] = $border_style;
		$return_data['row_table'] = $row_table;
		$return_data['col_table'] = $col_table;
		
	
		$file = $this->file_model->get_file_by_file_id($file_id);
		$file_content = $file->content;
		
	
		$json_decoded = json_decode($file->content);
		
		$drawing_object = new stdClass();
		$drawing_object->width = $width;
		$drawing_object->height = $height; 
		$drawing_object->posx = $posx;
		$drawing_object->posy = $posy; 
		$drawing_object->last_update = time();
		$drawing_object->id = $object_id; 
		$drawing_object->type = "table";
		$drawing_object->object_type = "table";
		$drawing_object->style = $style;
		$drawing_object->thickness = $thickness;
		$drawing_object->content_table = $content_table;
		$drawing_object->created_by = $username;
		$drawing_object->created_date = $curr_date;
		$drawing_object->border_style = $border_style;
		$drawing_object->row_table = $row_table;
		$drawing_object->col_table = $col_table;
		
		
		foreach($json_decoded->pages as $page)
		{
			if($page->id == $page_id)
			{
				array_push($page->drawing_objects,$drawing_object);
			}
		}
		
		
		$json_encoded = json_encode($json_decoded);
		
		$saved_file["content"] = $json_encoded;
		$this->file_model->save($saved_file, $file_id);
		
		$drawing_object = json_encode($drawing_object);
		//$saved_file_h["history_id"] 			= 1;
		$saved_file_h["file_id"] 			= $file_id;
		$saved_file_h["operation"] 			= "ADD";
		$saved_file_h["updated_time"] 		= time();
		$saved_file_h["changed_content"] 	= $drawing_object;
		$saved_file_h["previous_content"] 	= "";
		$saved_file_h["changed_object_id"] 	= $object_id;
		$saved_file_h["page_id"] 	= $page_id;
		$user = $this->ion_auth->user()->row();
		$saved_file_h["user_id"] 	= $user->id;

		//print_r($saved_file_h);
		// $this->file_model->save_file_history($saved_file_h);
		$h_id = $this->file_model->save_file_history($saved_file_h);
		$saved_file_h["history_id"] = $h_id;
		 // echo json_encode($return_data);
		echo json_encode(array('status'=>true,'message'=> json_encode($return_data),'history'=>$saved_file_h));
	}
	
	public function add_save_as()
	{
		$file_id_source = $this->input->post("file_id_source");
		$file_id_target = $this->input->post("file_id_target");
		$is_template = $this->input->post("is_template");
	
		$file_source = $this->file_model->get_file_by_file_id($file_id_source);
		$file_source_content = $file_source->content;
		
		$file_target = $this->file_model->get_file_by_file_id($file_id_target);
		$file_target_content = $file_target->content;
		
		$json_decoded_source = json_decode($file_source_content);
		$json_decoded_target = json_decode($file_target_content);
		
		unset($json_decoded_target);
		
		$json_decoded_target = $json_decoded_source; 
		
		$source = 'documents/'.$file_id_source;
		$dest = 'documents/'.$file_id_target;
		
		$dir = dir($source);
		while (false !== $entry = $dir->read()) {
			// Skip pointers
			if ($entry == '.' || $entry == '..') {
				continue;
			}
	 
			// Deep copy directories
			copy("$source/$entry", "$dest/$entry");
		}
	 
		// Clean up
		$dir->close();

		$update_time = time();
		$fileNameFromSrv = null;
		$stringParts = null;
		foreach($json_decoded_target->pages as $page_key => $single_page)
		{ 
				foreach($single_page->drawing_objects as $drawing_key => $single_drawing_object)
				{
					if($is_template)
					{
						$single_drawing_object->isTemplate=1;
					}
					if($single_drawing_object->type == "image" || $single_drawing_object->type == "attach" || $single_drawing_object->type == "chart")
					{ 
						$fileNameFromSrv = $single_drawing_object->image_url; 

						$stringParts = explode("/", $fileNameFromSrv);  
						
						$single_drawing_object->image_url = "documents/". $file_id_target."/".array_pop($stringParts);
						  
                        
					}
				} 
		}
		//copy history
		$this->file_model->copy_history($file_id_source,$file_id_target);
		$new_content = json_encode($json_decoded_target);
		//$file->content = $new_content; 
		
		$saved_file["content"] = $new_content;
		$saved_file["is_template"] = $is_template;
		$this->file_model->save($saved_file, $file_id_target);
		 
	} 
	
	public function delete_session()
	{
		$this->session->sess_destroy(); 
		echo "Done";
	}
	
	public function generate_file_html()
	{
		$fil_loc = $this->input->post("fil_loc");
		$str_html = $this->input->post("str_html"); 
		
		//open file
		$fp=fopen($fil_loc,"wb");
		fwrite($fp,$str_html);
		fclose($fp);
	}

}
